package com.json.parse;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.HashSet;
import java.util.Set;

import org.junit.Test;

import com.alibaba.fastjson.JSONReader;
import com.json.entity.Basic;

public class jsonParse {
	public static String path = "C:/candidates-data/zhibang-12kw.tar/zhihuan";
	public static Set<String> set = new HashSet<String>();

	public static void main(String[] args) {
		jsonParse jp = new jsonParse();
		jp.readFile(path);
		// jp.test();
	}

	@Test
	public void readFile(String path) {
		File file = new File(path);
		System.out.println(file.getAbsolutePath());
		if (file.exists()) {
			// 判断是目录还是文件
			if (file.isDirectory()) {// 如果是目录
				// 进入目录读出所有文件
				System.out.println(file.getName());

				for (String filename : file.list()) {

					File file2 = new File(path + File.separator + filename);
					if (file2.isDirectory()) {
						readFile(file2.getAbsolutePath());
					} else { // 如果是文件
						System.out.println(file2.getName());
						jsonParse(file2);
						
					}

				}
			} else {
				System.out.println("文件！");
			}

		}

	}

	@Test
	public void test() {
		String path = "C:/candidates-data/zhibang-12kw.tar/zhihuan/88";
		File file = new File(path);
		jsonParse(file);

	}

	public void jsonParse(File file) {
		System.out.println("-------------");
		JSONReader reader;
		try {
			reader = new JSONReader(new FileReader(file));
			reader.startObject();
			while (reader.hasNext()) {
				String key = reader.readString();
				System.out.println("**" + key);
				if (key.equals("responseHeader")) {
					reader.startObject();
					while (reader.hasNext()) {
						String key2 = reader.readString();
						if (key2.equals("status")) {
							System.out.println(key2 + ":" + reader.readObject());
						} else if (key2.equals("QTime")) {
							System.out.println(key2 + ":" + reader.readObject());
						} else if (key2.equals("params")) {
							System.out.println(key2 + ":" + reader.readObject());

						}

					}
					reader.endObject();
				} else if (key.equals("response")) {
					reader.startObject();
					while (reader.hasNext()) {
						String key2 = reader.readString();
						if (key2.equals("numFound")) {
							System.out.println(key2 + ":" + reader.readObject());
						} else if (key2.equals("start")) {
							System.out.println(key2 + ":" + reader.readObject());
						} else if (key2.equals("docs")) {
							reader.startArray();

							// set

							while (reader.hasNext()) {
								try {
									// String key3 = reader.readString();
									// System.out.println("--------");
									// System.out.println(reader.readObject());
									// Basic basic =
									// reader.readObject(Basic.class);
									// System.out.println(basic);

									
									
									//System.out.println(reader.readObject());
									Basic basic = reader.readObject(Basic.class);
									System.out.println(basic);
									//
									// }
								} catch (Exception e) {
									// TODO Auto-generated catch block
									e.printStackTrace();
								}

							}

							reader.endArray();
							System.out.println(set);
							System.out.println(set.size());

						}

					}

					reader.endObject();

				}

			}
			// System.exit(0);

		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}

	}

	private void write2Txt(String key, String value) {
		// 基本信息
		if (key.equals("id")) {

		} else if (key.equals("id")) {

		}

	}

}
